import 'package:flutter/material.dart';
import 'package:flutter_todolist/db/table/user_table.dart';
import 'package:flutter_todolist/pages/auth/auth_login.dart';
import 'package:flutter_todolist/pages/home/home.dart';
import 'package:flutter_todolist/pages/on_boarding/onboarding.dart';
import 'package:get/get.dart';
import 'package:package_info_plus/package_info_plus.dart';

import '../../db/db.dart';
import '../../utils/get_storage.dart';

class LaunchPage extends StatefulWidget {
  const LaunchPage({Key? key}) : super(key: key);

  @override
  State<LaunchPage> createState() => _LaunchPageState();
}

class _LaunchPageState extends State<LaunchPage> {

  @override
  void initState() {
    init();
    super.initState();
  }

  init() async {
    await ToDoDB().initDb();
    await AppStorage().init();
    PackageInfo info = await PackageInfo.fromPlatform();

    redirect();
  }

  redirect() async {
    bool isFirstOpen = AppStorage().read(AppStorage.FirstOpen) ?? true;
    if (isFirstOpen) {
      Get.off(() => const OnBoarding());
    } else {
      Map? userInfoMap = await AppStorage().read(AppStorage.UserInfo);
      if (userInfoMap != null) {
        Get.offAll(() => const Home());
      } else {
        Get.offAll(() => AuthPage());
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
